const _body_el = $('body');

window.smoothScroll = (e_id) => {
    let el;
    const cle = ()=>{
        let currentScroll= el.scrollTop();
        if (currentScroll > 0) {
            window.requestAnimationFrame(cle);
            el.scrollTop(currentScroll - (currentScroll / 5));
        }
    };
    if(e_id){
        el = $(e_id)
    }else {
        el = _body_el
    }
    cle();
};
export const scrollTop = function (e_id) {
    if(e_id){
        window.smoothScroll(e_id);
    }else {
        window.smoothScroll();
    }
};
export const eachParents = function (e,dom) {
    if(e.parentNode==dom){
        return true;
    }else {
        if(e.parentNode.nodeName.toLowerCase()=='body'){
            return false
        }else {
            return eachParents(e.parentNode,dom);
        }
    }
};
export const bodyOneClick = function (dom,callback) {
    const one = function () {
        _body_el.one('click',(e)=>{
            if(eachParents(e.target,dom)){
                one();
            }else {
                callback();
            }
        })
    };
    one();
};

export const bodySetOverflow = function (state) {
    state?_body_el.addClass('overflow-hid'):_body_el.removeClass('overflow-hid');
};

export const html = {
    encode: function (html) {
        //1.首先动态创建一个容器标签元素，如DIV
        var temp = document.createElement("div");
        //2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐，google支持)
        (temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html);
        //3.最后返回这个元素的innerHTML，即得到经过HTML编码转换的字符串了
        var output = temp.innerHTML;
        temp = null;
        return output;
    },
    /*2.用浏览器内部转换器实现html解码*/
    decode: function (text) {
        //1.首先动态创建一个容器标签元素，如DIV
        var temp = document.createElement("div");
        //2.然后将要转换的字符串设置为这个元素的innerHTML(ie，火狐，google都支持)
        temp.innerHTML = text;
        //3.最后返回这个元素的innerText(ie支持)或者textContent(火狐，google支持)，即得到经过HTML解码的字符串了。
        var output = temp.innerText || temp.textContent;
        temp = null;
        return output;
    },
    /*3.用正则表达式实现html转码*/
    encodeByRegExp:function (str){
        var s = "";
        if(str.length == 0) return "";
        s = str.replace(/&/g,"&amp;");
        s = s.replace(/</g,"&lt;");
        s = s.replace(/>/g,"&gt;");
        s = s.replace(/ /g,"&nbsp;");
        s = s.replace(/\'/g,"&#39;");
        s = s.replace(/\"/g,"&quot;");
        return s;
    },
    /*4.用正则表达式实现html解码*/
    decodeByRegExp:function (str){
        var s = "";
        if(str.length == 0) return "";
        s = str.replace(/&amp;/g,"&");
        s = s.replace(/&lt;/g,"<");
        s = s.replace(/&gt;/g,">");
        s = s.replace(/&nbsp;/g," ");
        s = s.replace(/&#39;/g,"\'");
        s = s.replace(/&quot;/g,"\"");
        return s;
    }
}